package com.itheima.boottlias.controller;

import com.itheima.boottlias.domain.pojo.Clazz;
import com.itheima.boottlias.domain.query.QueryParam;
import com.itheima.boottlias.domain.vo.PageResult;
import com.itheima.boottlias.domain.vo.Result;
import com.itheima.boottlias.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @since 2025-07-14 12:16
 */
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public Result pageQuery(QueryParam queryParam) {
        PageResult<Clazz> pageResult = clazzService.pageQuery(queryParam);
        return Result.success(pageResult);
    }

    @GetMapping("/list")
    public Result list(){
        List<Clazz> clazzList = clazzService.list();
        return Result.success(clazzList);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable(name = "id") Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(name = "id") Integer id){
        Clazz clazz = clazzService.findById(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        clazzService.updateById(clazz);
        return Result.success();
    }

}
